package com.daqian.mall.controller;

import com.daqian.mall.rest.RestResult;
import com.daqian.mall.service.CategoryService;
import com.daqian.mall.utils.CustomThreadPoolExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 根据一级类目同步其他类目信息
     *
     * @return
     */
    @GetMapping("/base/category/sync")
    public RestResult syncCategory() {
        new CustomThreadPoolExecutor().syncCategory();
        return RestResult.success();
    }

    /**
     * 获取一级目录列表
     *
     * @return
     */
    @GetMapping("/base/category/root")
    public RestResult listRoot() {
        return RestResult.success(categoryService.listRoot());
    }

    /**
     * 根据pid获取下级类目列表
     *
     * @param pid 父id
     * @return
     */
    @GetMapping("/base/category/sublist/{pid}")
    public RestResult listSubListByParentId(@PathVariable("pid") Long pid) {
        return RestResult.success(categoryService.listSubListByParentId(pid));
    }


    /**
     * 根据类目id获取类目属性
     *
     * @param id
     * @return
     */
    @GetMapping("/base/category/attr/{id}")
    public RestResult getAttrById(@PathVariable("id") Long id) {
        return RestResult.success(categoryService.getAttrById(id));
    }


    /**
     * 根据类目id获取类目规格
     *
     * @param id
     * @return
     */
    @GetMapping("/base/category/spec/{id}")
    public RestResult getSpecById(@PathVariable("id") Long id) {
        return RestResult.success(categoryService.getSpecById(id));
    }

}
